#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    initMainWindow();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void  MainWindow::initMainWindow()
{
    ui->toolBox->setCurrentIndex(0);
    QSqlQueryModel *categoryModel = new QSqlQueryModel(this);
    categoryModel->setQuery("select Name from category");
    qDebug()<< " set query \n";

    ui->newCategoryComboBox->setModel(categoryModel);
    commodity_model = new QSqlTableModel(this);
    commodity_model->setTable("commodity_inf");
    commodity_model->select();
    ui->commodityTableView->setModel(commodity_model);
    //ui->preCategoryComboBox->setModel(categoryModel);
    myMemberID = "b02020622";
    myOrdered = true;
    myPaySum = 0 ;
  //  QListWidgetItem *title = new QListWidgetItem();
   // title->setText(QString (" order id %1").arg(myOrderID));
   // title->setTextAlignment(Qt::AlignCenter);

}


void MainWindow::onTableSelectChange(int row)
{
   int r = 1 ;
   if(row != 0 )
       r = ui->commodityTableView->currentIndex().row();
   QModelIndex index;
   index = commodity_model->index(r,0);
   ui->newNameLineEdit->setText(commodity_model->data(index).toString());
   index = commodity_model->index(r,1);
   ui->newInputPriceLineEdit->setText(commodity_model->data(index).toString());
   index = commodity_model->index(r,2);
   ui->newoutputPriceLineEdit->setText(commodity_model->data(index).toString());
   showCommodityPhoto();
   QSqlQuery query;
   query.exec(QString("select Name from category where CategoryID = (select CategoryID from commodity where Name='%1')").arg(ui->newNameLineEdit->text()));
   query.next();
   ui->newCategoryComboBox->setCurrentText(query.value(0).toString());

}

void MainWindow::on_commodityTableView_clicked(const QModelIndex &index)
{
    onTableSelectChange(1);
}

void MainWindow::showCommodityPhoto()
{
    QPixmap photo;
    QModelIndex index;
    QSqlQueryModel *pictureModel = new QSqlQueryModel(this);
    QString name = ui->newNameLineEdit->text();
    qDebug()<< "show photo \n" ;
    pictureModel->setQuery("select Picture from commodity where Name='" +name+ "'");
    index = pictureModel->index(0,0);
    qDebug()<< "show after\n" ;
    photo.loadFromData(pictureModel->data(index).toByteArray(),"JPG");
    ui->newPictureLabel->setPixmap(photo);


}
/*
void MainWindow::loadPreCommodity()
{
    QSqlQueryModel *preCommodityModel = new QSqlQueryModel(this);
    QString name = ui->

}
void MainWindow::onPreNameComboBoxChange()
{

}
*/

//void on_commodityTableView_clicked(const QModelIndex &index)
//void on_preCategoryCombo_currentIndexChanged(int index);
void MainWindow::on_newUploadPushButton_clicked()
{
    QString picturename = QFileDialog::getOpenFileName(this, "choose the goods pic", ".","Image File(*.png *.jpg *.jpeg *.bmp)");
    if(picturename.isEmpty())
        return;
    myPicImag.load(picturename);
    ui->newPictureLabel->setPixmap(QPixmap::fromImage(myPicImag));

}
//callback of input store pushbutton
void MainWindow::on_newPutinStorePushButton_clicked()
{
   QSqlQuery query;
   query.exec(QString("select CategoryID from category where Name ='%1'").arg(ui->newCategoryComboBox->currentText()));
   query.next();
   int categoryid = query.value(0).toInt();
   qDebug()<< "category:-> " << categoryid;
   QString name = ui->newNameLineEdit->text();
   float inputprice = ui->newInputPriceLineEdit->text().toFloat();
   float outputprice = ui->newoutputPriceLineEdit->text().toFloat();
   int count = ui->newCountSpinBox->value();
   qDebug()<< "sname"<<name<<inputprice<<outputprice<<count ;
   bool flag =query.exec(QString("insert into commodity(CategoryID,Name,Picture,InputPrice,OutputPrice,Amount) values(%1,'%2',NULL,%3,%4,%5)").arg(categoryid).arg(name).arg(inputprice).arg(outputprice).arg(count));
  if(!flag)
    qDebug()<<"failed insert \n" ;
   QByteArray picdata;
   QBuffer buffer(&picdata);
   buffer.open(QIODevice::WriteOnly);
   myPicImag.save(&buffer,"JPG");
   QVariant var(picdata);
    qDebug()<<"insert pic "<<name ;
   QString sqlstr ="update commodity set Picture=? where Name ='"+name+"'";
   query.prepare(sqlstr);
   query.addBindValue(var);
   if(!query.exec())
   {
       QMessageBox::information(0,QObject::tr("remind"),"write failed");
   }
   else
   {
       qDebug()<<"insert pic success";
   }
   commodity_model->setTable("commodity_inf");
   commodity_model->select();
   ui->commodityTableView->setModel(commodity_model);
     qDebug()<< "refresh";
}
//
void MainWindow::on_newClearancePushButton_clicked()
{
   QSqlQuery query;
   query.exec(QString("delete form commodity where Name = '%1'").arg(ui->newNameLineEdit->text()));//del commodity
   //refresh gui
   ui->newNameLineEdit->setText("");
   ui->newInputPriceLineEdit->setText("");
   ui->newoutputPriceLineEdit->setText("");
   ui->newCountSpinBox->setValue(1);
   ui->newPictureLabel->clear();
   commodity_model->setTable("commdity_inf");
   commodity_model->select();
   ui->commodityTableView->setModel(commodity_model);

}
